package com.rxjava.test.now;


import java.net.URI;
import java.util.Collections;
import java.util.List;

public class CatsHelper {
    ApiWrapper apiWrapper;

    public AsynJob<URI> saveTheCutestCat(String query) {
        AsynJob<List<Cat>> catsListAsynJob = apiWrapper.queryCats(query);

        AsynJob<Cat> cutestCatAsynJob = catsListAsynJob.map(new Func<List<Cat>, Cat>() {
            @Override
            public Cat call(List<Cat> cats) {
                return findCutest(cats);
            }
        });

        AsynJob<URI> uriAsynJob = cutestCatAsynJob.flatMap(new Func<Cat, AsynJob<URI>>() {
            @Override
            public AsynJob<URI> call(Cat cat) {
                return apiWrapper.store(cat);
            }
        });

        return uriAsynJob;

    }

    private Cat findCutest(List<Cat> cats) {
        return Collections.max(cats);
    }
}
